package org.odefu.model.project;

import java.util.HashMap;
import java.util.Map;

public final class Row implements Named
{
	private final Map<String, Object> values = new HashMap<String, Object>();

	private final String name;

	/**
	 * Construct a new row with the given name.
	 * 
	 * @param name The name for this row.
	 */
	public Row(final String name)
	{
		this.name = name;
	}

	/**
	 * Get the name of this row.
	 */
	@Override
	public String name()
	{
		return name;
	}

	/**
	 * Gets a value with the given id.
	 * 
	 * @param id the ID which value is fetched
	 * @return The value for the id
	 */
	public Object value(final String id)
	{
		return values.get(id);
	}

	/**
	 * Set a new value for the given id.
	 * 
	 * @param id The key
	 * @param value The value for the key
	 */
	public void value(final String id, final Object value)
	{
		values.put(id, value);
	}

	/**
	 * Checks if this row contains the given id.
	 * 
	 * @param id
	 * @return
	 */
	public boolean contains(final String id)
	{
		return values.containsKey(id);
	}

	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = 1;
		result = prime * result + (name == null ? 0 : name.hashCode());
		result = prime * result + (values == null ? 0 : values.hashCode());
		return result;
	}

	@Override
	public boolean equals(final Object obj)
	{
		if (this == obj)
		{
			return true;
		}
		if (obj == null)
		{
			return false;
		}
		if (!(obj instanceof Row))
		{
			return false;
		}
		final Row other = (Row) obj;
		if (name == null)
		{
			if (other.name != null)
			{
				return false;
			}
		}
		else if (!name.equals(other.name))
		{
			return false;
		}
		if (values == null)
		{
			if (other.values != null)
			{
				return false;
			}
		}
		else if (!values.equals(other.values))
		{
			return false;
		}
		return true;
	}
}
